package com.weixing.mall.provider.controller.common;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.provider.api.model.CalcProfitPo;
import com.weixing.mall.provider.api.model.vo.AgentRpcVo;
import com.weixing.mall.provider.api.service.AgentServiceRpc;
import com.weixing.mall.provider.model.domain.Order;
import com.weixing.mall.provider.model.vo.order.NoAuditVo;
import com.weixing.mall.provider.service.IOrderService;
import com.weixing.mall.provider.util.OrderUtil;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 订单审核操作通用接口
 *
 * @author  siashan
 * @date  2019/10/15
 * @since  1.0
 **/
@RestController
@RequestMapping("/order/audit/common/")
public class OrderAuditCommonController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private AgentServiceRpc agentServiceRpc;

    /**
     * 订单待审核人员列表
     * @param id  订单id
     * @return  待审核人员列表
     */
    @GetMapping("/{id}")
    public Result<List<NoAuditVo>> notAuditList(@PathVariable Long id) {
        Order order = orderService.getById(id);
        String agentCliamLine = order.getAgentCliamLine();
        List<NoAuditVo> list = Lists.newArrayList();
        if (StringUtil.isNotEmpty(agentCliamLine)) {
            String orderAuditorList = order.getOrderAuditorList();
            if (StringUtil.isNotEmpty(orderAuditorList)) {
                String[] split = orderAuditorList.split(",");
                List<String> auditedList = new ArrayList<String>(Arrays.asList(split));
                if (CollUtil.isNotEmpty(auditedList)) {
                    auditedList.remove(0);
                }
                List<CalcProfitPo> calcProfitPos = OrderUtil.parseTreePath(agentCliamLine);
                for (CalcProfitPo po : calcProfitPos) {
                    if (CollUtil.isNotEmpty(auditedList)) {
                        String aid = String.valueOf(po.getAgentId());
                        if (auditedList.contains(aid)) {
                            continue;
                        }
                        Result<AgentRpcVo> agentRpcVoResult = agentServiceRpc.selectByAgentId(po.getAgentId());
                        if (!agentRpcVoResult.isSuccess()) {
                            return ResultUtil.error("调用代理服务失败，请联系管理员解决");
                        } else {
                            AgentRpcVo data = agentRpcVoResult.getData();
                            NoAuditVo vo = new NoAuditVo();
                            vo.setId(data.getId());
                            vo.setName(data.getAgentName());
                            list.add(vo);
                        }
                    }

                }
            }
        }
        return ResultUtil.success(list);
    }


}
